package com.shenfa.dao;

import java.sql.SQLException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.ibatis.sqlmap.client.SqlMapClient;
import com.shenfa.bean.Paper;
import com.shenfa.bean.PaperCityCrd;
import com.shenfa.bean.PaperInfo;
import com.shenfa.bean.TreeNode;


public class PaperDao {
	
	@Autowired
	private SqlMapClient sqlMapClient;

	/**
	 * 根据卡产品id查询预印纸
	 * 
	 * @param cardid
	 * @param skipResults
	 * @param maxResults
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<Paper> queryGridList(String cardid,
			int skipResults, int maxResults) {
		try {
			return sqlMapClient.queryForList(
					"paper_manager.query_paperofcrid", cardid, skipResults,
					maxResults);
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public int queryCount(String cardid) {
		try {
			return (Integer)sqlMapClient.queryForObject(
					"paper_manager.queryCount", cardid);
		} catch (SQLException e) {
			e.printStackTrace();
			return -1;
		}
	}
	
	/**
	 * 根据预印纸id查询信息从表list
	 * @param paperid
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<PaperInfo> queryPaperInfo(String paperid){
		try {
			return sqlMapClient.queryForList("paper_manager.query_paperInfo", paperid);
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
	}		
	
	/**
	 * 根据预印纸编号查询对应预印纸id的从表
	 * @param paperNo
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<PaperInfo> queryPaperInfoBypaperNo(String paperNo) {
		try {
			return sqlMapClient.queryForList("paper_manager.query_paperInfoPNo", paperNo);
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	/**
	 * 查询单个预印纸产讯从表
	 * @param p
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<PaperInfo> queryAPaperInfo(PaperInfo p){
		try {
			return sqlMapClient.queryForList("paper_manager.query_aPaperInfo", p);
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	/**
	 * 根据卡产品id查询城市id
	 * @param role
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<String> qurcitybycard(String card) {
		try {
			return sqlMapClient.queryForList("paper_manager.query_city",card);
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	/**
	 * 根据卡产品id查询城市列表Tree
	 * @author zys(2011-09-14)
	 * 
	 * @param cardid
	 * @return
	 * @throws SQLException 
	 */
	@SuppressWarnings("unchecked")
	public List<TreeNode> queryCityByCard(PaperCityCrd pcc) throws SQLException{
		 return sqlMapClient.queryForList("paper_manager.query_cityByCardList", pcc);		
	}
	
	/**
	 * 根据卡产品id以及预印纸id查询城市列表Tree
	 * @author zys(2011-09-14)
	 * 
	 * @param cardid
	 * @param printId
	 * @return
	 * @throws SQLException
	 */
	@SuppressWarnings("unchecked")
	public List<TreeNode> queryCityByCardPrint(PaperCityCrd pcc) throws SQLException {
		return sqlMapClient.queryForList("paper_manager.query_cityByCardList", pcc);	
	}
	
	/**
	 * 根据卡产品id和预印纸的有效时间查询预印纸
	 * @param pcc
	 * @param p
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<Paper> queryPaperByTimeCard(Paper p) {
		try {
			return sqlMapClient.queryForList("paper_manager.queryByTimeCard", p);
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	/**
	 * 根据预印纸id查询预印纸
	 * @param p
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<Paper> queryPaperByPaperid(Paper p) {
		try {
			return sqlMapClient.queryForList("paper_manager.query_samePaper", p);
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	/**
	 * 根据预印纸编号查询单个预印纸
	 * @param paperNo
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<Paper> queryPaper(String paperNo) {
		 try {
			return sqlMapClient.queryForList("paper_manager.query_papName_papId", paperNo);
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	/**
	 * 添加对应卡产品的预印纸
	 * @author zys(2011-09-15)
	 * 
	 * @param paperbean
	 * @return
	 */
	public int addPaper(Paper paperbean) {
		try {
			sqlMapClient.insert("paper_manager.addPaper",paperbean);
		} catch (SQLException e) {
			e.printStackTrace();
			return -1;
		} 
		return 0;
	}
	
	/**
	 * 修改更新对应卡产品的预印纸
	 * @author zys(2011-09-15)
	 * 
	 * @param paperbean
	 * @return
	 */
	public int updPaper(Paper paperbean) {
		try {
			sqlMapClient.update("paper_manager.updPaper", paperbean);
		} catch (SQLException e) {
			e.printStackTrace();
			return -1;
		}
		return 0;
	}
	
	/**
	 * 解除预印纸与外包商的关联关系
	 * @author zys(2011-09-20)
	 * 
	 * @param paperbean
	 * @return
	 */
	public int delPaperRelative(PaperCityCrd paperCityCrdBean) {
		try {
			sqlMapClient.delete("paper_manager.delPaperForBusi", paperCityCrdBean);
		} catch (SQLException e) {
			e.printStackTrace();
			return -1;
		}
		return 0;
	}
	
	/**
	 * 添加对应卡产品的预印纸信息从表
	 * @author zys(2011-09-15)
	 * @param pInfo
	 * @return
	 */
	public int addPaperInfo(PaperInfo pInfo) {
		try {
			sqlMapClient.insert("paper_manager.add_paperInfo", pInfo);
		} catch (SQLException e) {
			e.printStackTrace();
			return -1;
		}
		return 0;
	}
	
	/**
	 * 修改更新对应卡产品的预印纸信息从表
	 * @author zys(2011-09-15)
	 * @param pInfo
	 * @return
	 */
	public int udpPaperInfo(PaperInfo pInfo) {
		try {
			sqlMapClient.update("paper_manager.upd_paperInfo", pInfo);
		} catch (SQLException e) {
			e.printStackTrace();
			return -1;
		}
		return 0;
	}
	
	/**
	 * 删除对应卡产品的预印纸信息主表
	 */
	public int delPaper(String paperId) {
		try {
			sqlMapClient.delete("paper_manager.del_paper", paperId);
		} catch (SQLException e) {
			e.printStackTrace();
			return -1;
		}
		return 0;
	}
	
	/**
	 * 删除对应卡产品的预印纸信息从表
	 * @author zys(2011-09-15)
	 * @param paperId
	 * @return
	 */
	public int delPaperInfo(String paperId) {
		try {
			sqlMapClient.delete("paper_manager.del_paperInfo", paperId);
		} catch (SQLException e) {
			e.printStackTrace();
			return -1;
		}
		return 0;
	}
	
	/**
	 *单独操作删除预印纸信息从表
	 * @param pInfo
	 * @return
	 */
	public int delPaperInfoSingle(PaperInfo pInfo) {
		try {
			sqlMapClient.delete("paper_manager.del_paperInfoSingle", pInfo);
		} catch (SQLException e) {
			e.printStackTrace();
			return -1;
		}
		return 0;
	}
	
	/**
	 * 根据预印纸ID删除从表信息
	 */
	public int delPaperInfoById(String pid) {
		try {
			sqlMapClient.delete("paper_manager.delPaperInfoById", pid);
		} catch (SQLException e) {
			e.printStackTrace();
			return -1;
		}
		return 0;
	}
	
	/**
	 * 预览文件
	 * @param id
	 * @return
	 */
	public String show(String id){
		Object obj=null;
		try {
			obj=sqlMapClient.queryForObject("paper_manager.showfile",id);
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
		if(null!=obj){
			return obj.toString();
		}
		return null;
	}

	//验证预印纸编号
	@SuppressWarnings("unchecked")
	public String checkPaperNo(String paperNo) {
		List<Object> list = null;
		try {
			list = sqlMapClient.queryForList("paper_manager.checkpaperno", paperNo);
			if(list.size() == 0) 
				return "0";
		} catch (SQLException e) {
			e.printStackTrace();
			return "1"; 
		}
		return "1";
	}
}
